'===============================================================================
' Author: Vincent Rothwell, http://blog.thekid.me.uk
'===============================================================================
' This file was taken from here: http://www.codeplex.com/sptemplateland
' It is provided under Microsoft Community License (Ms-CL)
'===============================================================================

Option Explicit
Dim oFS, oManifest, args, oFestures, iFeatureCount

dim arFeatures(20)
iFeatureCount = 0

set args = WScript.Arguments

' Create the File System Object
Set oFS = CreateObject("Scripting.FileSystemObject")

Set oManifest = oFS.CreateTextFile(args(0) + "manifest.xml", true)
oManifest.WriteLine "<Solution xmlns='http://schemas.microsoft.com/sharepoint/' SolutionId='CBCCB717-A86C-4b86-A40A-18F16F159980'>"

dim oFile, oGacFolder, oBinFolder, oSub
set oGacFolder = oFS.GetFolder(args(0) + "DLLS\GAC")
set oBinFolder = oFS.GetFolder(args(0) + "DLLS")

            dim sSafeControls, oSafeXml

if (oGacFolder.Files.Count > 0 or oBinFolder.Files.Count > 0) then
    oManifest.WriteLine "    <Assemblies>"

    for each oFile in oGacFolder.Files
        oManifest.WriteLine "        <Assembly DeploymentTarget='GlobalAssemblyCache' Location='GAC\" + oFile.Name + "'>"
        if (oFS.FileExists(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")) then
            set oSafeXml = oFS.OpenTextFile(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")
            
            sSafeControls = oSafeXml.ReadAll
            oManifest.Write Mid(sSafeControls, 4)
            
            oManifest.WriteBlankLines 1
            oSafeXml.Close
        end if
        oManifest.WriteLine "        </Assembly>"
    next

    for each oFile in oBinFolder.Files
        oManifest.WriteLine "        <Assembly DeploymentTarget='WebApplication' Location='" + oFile.Name + "'>"
        if (oFS.FileExists(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")) then
            set oSafeXml = oFS.OpenTextFile(args(0) + "DLLS\SafeControls\" + oFile.Name + ".xml")
            
            sSafeControls = oSafeXml.ReadAll
            oManifest.Write Mid(sSafeControls, 4)
            
            oManifest.WriteBlankLines 1
            oSafeXml.Close
        end if
        oManifest.WriteLine "        </Assembly>"
    next
    
    for each oSub in oBinFolder.SubFolders
        if (oSub.Name <> ".svn") then
			for each oFile in oSub.Files
				oManifest.WriteLine "        <Assembly DeploymentTarget='WebApplication' Location='" + oSub.Name + "\" + oFile.Name + "'>"
				oManifest.WriteLine "        </Assembly>"
			next
		end if
    next

    oManifest.WriteLine "    </Assemblies>"
end if
oManifest.WriteLine "    <TemplateFiles>"

' Add the files in the manifest

EnumFolder args(0) + "TEMPLATE", "TEMPLATE"

oManifest.WriteLine "    </TemplateFiles>"

oManifest.WriteLine "    <FeatureManifests>"

' Add the features
dim sFeature

for each sFeature in arFeatures
    if (sFeature <> "") then oManifest.WriteLine sFeature
next

oManifest.WriteLine "    </FeatureManifests>"

if (oFS.FileExists(args(0) + "cas.xml")) then
    dim sCasXML, oCasFile
    set oCasFile = oFS.OpenTextFile(args(0) + "cas.xml")
    
    sCasXml = oCasFile.ReadAll
    oManifest.Write Mid(sCasXml, 4)
    
    oManifest.WriteBlankLines 1
    oCasFile.Close
end if


oManifest.WriteLine "</Solution>"

oManifest.Close

sub EnumFolder(sFolder, sRelativePath)
    
    dim oFolder, oFolders, oSub, oFile
    set oFolder = oFS.GetFolder(sFolder)
    
    for each oFile in oFolder.Files
        
        dim sTestFolder, iPos
        
        iPos = InStrRev(sFolder, "TEMPLATE\FEATURES")
        if (iPos > 0) then
            sTestFolder = Mid(sFolder, iPos + 18)
        else
            sTestFolder = sFolder
        end if

        if (InStr(1, sTestFolder, "\") > 0) then
            if (InStr(1, sFolder, "FEATURES") = 0) then
                oManifest.WriteLine "        <TemplateFile Location='" + sRelativePath + "\" + oFile.Name + "' />"
            else
                oManifest.WriteLine "        <TemplateFile Location='FEATURES\" + sRelativePath + "\" + oFile.Name + "' />"
            end if               
        end if
        
        if (lcase(oFile.Name) = "feature.xml") then
            arFeatures(iFeatureCount) = "        <FeatureManifest Location='" + sRelativePath + "\" + oFile.Name + "' />"
            iFeatureCount = iFeatureCount + 1
        end if
        
    next

    if (sRelativePath = "TEMPLATE") then sRelativePath = ""
    if (sRelativePath = "FEATURES") then sRelativePath = ""

    if (sRelativePath <> "") then sRelativePath = sRelativePath + "\"

    for each oSub in oFolder.SubFolders
        if (oSub.Name <> ".svn") then
           EnumFolder oSub.Path, sRelativePath + oSub.Name
        end if
    next
    
end sub